<template>
  <pro-table
    ref="table"
    title="列表"
    :request="getList"
    :columns="columns"
    :search="searchConfig"
    :pagination="paginationConfig"
    @selectionChange="handleSelectionChange"
  >
    <!-- 工具栏 -->
    <template #toolbar>
      <el-button type="primary" icon="el-icon-refresh" @click="refresh">
        刷新
      </el-button>
    </template>
    <template #operate="scope">
      <el-button
        size="mini"
        type="primary"
        @click="$router.push(`/users/edit/${scope.row.id}`)"
      >
        Edit
      </el-button>
<!--      <el-button size="mini" type="danger" @click="delAdmin(scope.role.id)">删除</el-button>-->
      <el-popconfirm title="Are you sure to delete this?" @confirm="delUsers(scope.row.id)">
        <template #reference>
          <el-button size="mini" type="danger">Delete</el-button>
        </template>
      </el-popconfirm>
    </template>
  </pro-table>
 </template>

<script>
import { defineComponent, onMounted, reactive, ref, toRefs } from "vue";
import { getUsersList, delUsers } from "../../api/users";
import { ElMessage } from "element-plus";


export default defineComponent({
  name: 'usersList',
  setup() {
    const state = reactive({
      // 表格列配置，大部分属性跟el-table-column配置一样
      columns: [
        { type: 'selection' },
        { label: '序号', prop: 'id' },
        { label: '游戏ID', prop: 'nickname', sortable: true, width: 180 },
        { label: '氏族', prop: 'team' },
        { label: 'QQ', prop: 'qq' },
        { label: '积分', prop: 'integral' },
        { label: '状态', prop: 'status' },
        {
          label: '操作',
          fixed: 'right',
          width: 180,
          align: 'center',
          tdSlot: 'operate', // 自定义单元格内容的插槽名称
        },
      ],
      // 搜索配置
      searchConfig: {
        labelWidth: '90px', // 必须带上单位
        inputWidth: '360px', // 必须带上单位
        fields: [
          {
            type: 'text',
            label: '游戏ID',
            name: 'nickname',
            defaultValue: '',
          },
          {
            label: '状态',
            name: 'status',
            type: 'select',
            defaultValue: '',
            options: [
              {
                name: '',
                value: '',
              },
              {
                name: '正常',
                value: 1,
              },
              {
                name: '冻结',
                value: 2,
              },
            ],
          },
          {
            label: '氏族',
            name: 'team',
            type: 'radio-button',
            options: [
              {
                name: '一团',
                value: 1,
              },
              {
                name: '二团',
                value: 2,
              },
              {
                name: '三团',
                value: 3,
              },
              {
                name: '四团',
                value: 4,
              },
            ],
          },
        ],
      },
      // 分页配置
      paginationConfig: {
        layout: 'total, prev, pager, next, sizes', // 分页组件显示哪些功能
        pageSize: 5, // 每页条数
        pageSizes: [5, 10, 15, 20, 50],
        style: { textAlign: 'left' },
      },
      selectedItems: [],
      batchDelete() {
        console.log(state.selectedItems)
      },
      // 选择
      handleSelectionChange(arr) {
        state.selectedItems = arr
      },
      // 请求函数
      async getList(params) {
        // params是从组件接收的，包含分页和搜索字段。
        const { data } = await getUsersList(params)
        // 必须要返回一个对象，包含data数组和total总数
        return {
          data: data.list,
          total: +data.total,
        }
      },
    })
    const table = ref(null)
    const refresh = () => {
      table.value.refresh()
    }
    const methods = {
      async delUsers(uid) {
        let resp = await delUsers(uid)
        if(resp.code !== 200){
          ElMessage({
            message: resp.message || '服务器错误！',
            type: 'success',
          })
        }else {
          table.value.refresh()
          ElMessage({
            message: resp.message,
            type: 'success',
          })
        }
      },
    }
    onMounted(() => {
      // methods.getUserList()
    })
    return { ...toRefs(state), refresh, table, ...methods }
  },
})
</script>
